//
//  AppDelegate.swift
//  SinaWeibo
//
//  Created by 王佳琳 on 16/2/16.
//  Copyright © 2016年 王佳琳. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        print(AccountViewModel.sharedAccountViewModel.userLogin)
        setThemeColor()
        //注册通知
        registerNotification()
       //实例化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        //可视化
        window?.makeKeyAndVisible()
        
        //设置跟视图控制器
        window?.rootViewController = defaultRootViewController()
        
        return true
    }
    //消息机制才能够找到该方法
    @objc private func AppRootViewController(n: NSNotification) {
        //根据通知的结果来显示欢迎页面还是tabbVC
        print("come here appdelegate")
        //三木运算简写
        window?.rootViewController = n.object == nil ? WelcomeViewController() : MainViewController()
    }
    
    //注册通知
    private func registerNotification() {
        //object 观测哪一个对象发出的通知  如果为nil 默认所有对象
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "AppRootViewController:", name: KChoseRootVC, object: nil)
    }
    
    private func defaultRootViewController() -> UIViewController {

        return AccountViewModel.sharedAccountViewModel.userLogin ? WelcomeViewController() : MainViewController()
    }

    private func setThemeColor() {
        //必须提前设置  在控件被实例化之前就需要设置渲染颜色
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor

    }
    //移除通知  编码规范而已
    //代理  对象是一个单例对象
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    }

